﻿using DG.Tweening;

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class UIBattleScrollClamp : UIObject, IEndDragHandler // required interface when using the OnEndDrag method.
{
    public int clampSize;
    public float gridNum;
    private float _clampSizeSum;
    ScrollRect _scrollRect;
    public float _ratio;
    public DiceType _diceType;

    public DiceType GetDiceType()
    {
        _ratio = 1 - _scrollRect.verticalNormalizedPosition + 0.2f;
        _diceType = (DiceType)Mathf.RoundToInt(_ratio / 0.2f);
        return _diceType;
    }
    protected override void Awake()
    {
        base.Awake();
        _scrollRect = this.GetComponent<ScrollRect>();
        _clampSizeSum = clampSize * gridNum - clampSize;
    }

    public void OnEndDrag(PointerEventData data)
    {

        var content = _scrollRect.content;
        //Debug.LogError("OnEndDrag " + this.name + "!" + data.position + " content:" + content.localPosition);
        var remaind = content.localPosition.y % clampSize;
        float targetValue;
        if(remaind > clampSize / 2)
        {
            //入
            targetValue = content.localPosition.y - remaind + clampSize;
        }
        else
        {
            //舍去
            targetValue = content.localPosition.y - remaind;
        }
       // 
       float verticalValue = 1 - targetValue / _clampSizeSum;
        _scrollRect.DOVerticalNormalizedPos(verticalValue, 0.2f);
    }


}
